package com.sk.weichat.ui.me.wallet.viewmode;

import android.util.Log;
import com.sk.weichat.bean.AliPayAuthMsgBean;
import com.sk.weichat.bean.IsBindAlipayBean;
import com.sk.weichat.ui.base.CoreManager;
import com.sk.weichat.ui.me.wallet.view.SetpswAuthenticationView;
import com.xuan.xuanhttplibrary.okhttp.HttpUtils;
import com.xuan.xuanhttplibrary.okhttp.callback.BaseCallback;
import com.xuan.xuanhttplibrary.okhttp.result.ObjectResult;
import java.util.HashMap;
import java.util.Map;
import okhttp3.Call;

public class SetpswAuthenticationViewMode {

    private SetpswAuthenticationView mSetpswAuthenticationView;
    private CoreManager coreManager;

    public SetpswAuthenticationViewMode( CoreManager coreManager,SetpswAuthenticationView mSetpswAuthenticationView) {
        this.mSetpswAuthenticationView = mSetpswAuthenticationView;
        this.coreManager = coreManager;
    }

    public void isBindAlipay(){
        mSetpswAuthenticationView.showProgress();
        Map<String, String> params = new HashMap<>();
        params.put("access_token", coreManager.getSelfStatus().accessToken);
        HttpUtils.post().url(coreManager.getConfig().IS_BIND_ALIPAY)
                .params(params)
                .build()
                .execute(new BaseCallback<IsBindAlipayBean>(IsBindAlipayBean.class) {
                    @Override
                    public void onResponse(ObjectResult<IsBindAlipayBean> result) {
                        mSetpswAuthenticationView.isBingAlipayResult(result);
                        mSetpswAuthenticationView.hideProgress();
                    }

                    @Override
                    public void onError(Call call, Exception e) {
                        Log.e("AlipayAutoMsg", "Exception", e);
                        mSetpswAuthenticationView.showLoadFailMsg(e.toString());
                        mSetpswAuthenticationView.hideProgress();
                    }
                });

    }

    /**
     * 获取支付宝授权信息绑定支付宝
     */
    public void getAlipayAutoMsg(){
        mSetpswAuthenticationView.showProgress();
        Map<String, String> params = new HashMap<>();
        params.put("access_token", coreManager.getSelfStatus().accessToken);
        HttpUtils.post().url(coreManager.getConfig().ALIPAY_AUTH_MSG)
                .params(params)
                .build()
                .execute(new BaseCallback<String>(String.class) {

                    @Override
                    public void onResponse(ObjectResult<String> result) {
                        mSetpswAuthenticationView.AlipayAuthMsgResult(result);
                        mSetpswAuthenticationView.hideProgress();
                    }
                    @Override
                    public void onError(Call call, Exception e) {
                        Log.e("AlipayAutoMsg", "Exception", e);
                        mSetpswAuthenticationView.showLoadFailMsg(e.toString());
                        mSetpswAuthenticationView.hideProgress();
                    }
                });
    }

    /**
     * 上传支付宝授权码
     * */
    public void setAlipayAutoCode(String autoCode){
        mSetpswAuthenticationView.showProgress();
        Map<String, String> params = new HashMap<>();
        params.put("access_token", coreManager.getSelfStatus().accessToken);
        params.put("code", autoCode);
        HttpUtils.post().url(coreManager.getConfig().ALIPAY_AUTH_CODE)
                .params(params)
                .build()
                .execute(new BaseCallback<String>(String.class) {

                    @Override
                    public void onResponse(ObjectResult<String> result) {
                        mSetpswAuthenticationView.AlipayAutoCodeResult(result);
                        mSetpswAuthenticationView.hideProgress();
                    }
                    @Override
                    public void onError(Call call, Exception e) {

                        Log.e("AlipayAutoMsg", "Exception", e);
                        mSetpswAuthenticationView.showLoadFailMsg(e.toString());

                        mSetpswAuthenticationView.hideProgress();
                    }
                });
    }


    /**
    * 比较支付宝是否一致
    * */
    public void compareAlipay(String code){
        mSetpswAuthenticationView.showProgress();
        Map<String, String> params = new HashMap<>();
        params.put("access_token", coreManager.getSelfStatus().accessToken);
        params.put("code", code);
        HttpUtils.post().url(coreManager.getConfig().ALIPAY_COMPARE)
                .params(params)
                .build()
                .execute(new BaseCallback<String>(String.class) {
                    @Override
                    public void onResponse(ObjectResult<String> result) {
                        mSetpswAuthenticationView.CompareAlipayResult(result);
                        mSetpswAuthenticationView.hideProgress();
                    }
                    @Override
                    public void onError(Call call, Exception e) {
                        Log.e("AlipayAutoMsg", "Exception", e);
                        mSetpswAuthenticationView.showLoadFailMsg(e.toString());
                        mSetpswAuthenticationView.hideProgress();
                    }
                });
    }
}
